package controller

import (
	"net/http"
	"think/app/model"
	"think/common"
)

// UserController 用户控制器
type UserController struct {
	common.Controller
}

// Index 用户管理首页
func (c *UserController) Index(w http.ResponseWriter, r *http.Request) {

	c.Success(w, map[string]string{
		"message": "User Management API",
		"version": "1.0.0",
	})
}

// List 获取用户列表
func (c *UserController) List(w http.ResponseWriter, r *http.Request) {
	userModel := model.NewUser()
	//C输出数据库是否链接成功
	if userModel == nil {
		c.Error(w, http.StatusInternalServerError, "数据库连接失败")
		return
	}
	//输出sql

	users := userModel.GetUsersOrderByScore(10)
	c.Success(w, users)
}

// Detail 获取用户详情
func (c *UserController) Detail(w http.ResponseWriter, r *http.Request) {
	c.Success(w, map[string]string{"message": "Get user detail API"})
}

// GetUsersByRole 按角色获取用户列表（演示GET方法前缀）
func (c *UserController) GetUsersByRole(w http.ResponseWriter, r *http.Request) {
	role := r.URL.Query().Get("role")
	if role == "" {
		role = "all"
	}
	c.Success(w, map[string]string{
		"message": "Get users by role",
		"role":    role,
	})
}

// PostCreateUser 创建用户（演示POST方法前缀）
func (c *UserController) PostCreateUser(w http.ResponseWriter, r *http.Request) {
	c.Success(w, map[string]string{
		"message": "User created successfully",
	})
}

// PutUpdateUser 更新用户（演示PUT方法前缀）
func (c *UserController) PutUpdateUser(w http.ResponseWriter, r *http.Request) {
	c.Success(w, map[string]string{
		"message": "User updated successfully",
	})
}

// DeleteUser 删除用户（演示DELETE方法前缀）
func (c *UserController) DeleteUser(w http.ResponseWriter, r *http.Request) {
	c.Success(w, map[string]string{
		"message": "User deleted successfully",
	})
}
